<script setup lang="ts">
import {onMounted, ref} from 'vue'
import {list, updateBook, deleteBook, createBook, uploadImage} from "@/api/user/register.ts";
import {ElMessage, ElMessageBox} from "element-plus";
import type { UploadProps } from 'element-plus'
import { UploadFilled, Edit, Loading, Delete } from '@element-plus/icons-vue'

// 搜索表单数据
const searchForm = ref({
  name: ''
})

interface Book{
  id: number;
  name: string;
  author: string;
  description: string;
  cover: string
}

const bookList = ref<Book[]>([])
const loading = ref(false)

// 批量删除相关
const selectedBooks = ref<Book[]>([])
const batchDeleting = ref(false)

// 添加弹窗相关
const addDialogVisible = ref(false)
const addForm = ref<Omit<Book, 'id'>>({
  name: '',
  author: '',
  description: '',
  cover: ''
})

// 详情弹窗相关
const detailDialogVisible = ref(false)
const currentBook = ref<Book | null>(null)

// 编辑弹窗相关
const editDialogVisible = ref(false)
const editForm = ref<Book>({
  id: 0,
  name: '',
  author: '',
  description: '',
  cover: ''
})

// 图片文件相关
const addImageFile = ref<File | null>(null)
const editImageFile = ref<File | null>(null)
const addImagePreview = ref('')
const editImagePreview = ref('')

// 表单验证规则
const formRules = {
  name: [
    { required: true, message: '请输入图书名称', trigger: 'blur' },
    { min: 1, max: 100, message: '长度在 1 到 100 个字符', trigger: 'blur' }
  ],
  author: [
    { required: true, message: '请输入作者', trigger: 'blur' },
    { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: 'blur' }
  ],
  description: [
    { required: true, message: '请输入图书描述', trigger: 'blur' },
    { min: 1, max: 150, message: '长度在 1 到 150 个字符', trigger: 'blur' }
  ],
  cover: [
    { required: true, message: '请选择封面图片', trigger: 'blur' }
  ]
}

const getBookList = async (name?: string) => {
  try{
    loading.value = true
    const res = await list(name);
    bookList.value = res.data
  }catch (e:any){
    ElMessage.error(e.response?.data?.message || e.message || '获取图书列表失败')
  } finally {
    loading.value = false
  }
}

// 搜索图书
const handleSearch = () => {
  console.log('搜索图书:', searchForm.value.name)
  getBookList(searchForm.value.name);
}

// 重置搜索
const handleReset = () => {
  searchForm.value.name = ''
  selectedBooks.value = []
  getBookList();
}

// 添加图书
const handleAdd = () => {
  addForm.value = {
    name: '',
    author: '',
    description: '',
    cover: ''
  }
  addImageFile.value = null
  addImagePreview.value = ''
  addDialogVisible.value = true
}

// 批量选择处理
const handleSelectionChange = (selection: Book[]) => {
  selectedBooks.value = selection
}

// 批量删除
const handleBatchDelete = async () => {
  if (selectedBooks.value.length === 0) {
    ElMessage.warning('请先选择要删除的图书')
    return
  }

  try {
    await ElMessageBox.confirm(
        `确定要删除选中的 ${selectedBooks.value.length} 本图书吗？`,
        '批量删除确认',
        {
          confirmButtonText: '确定删除',
          cancelButtonText: '取消',
          type: 'warning',
        }
    )

    batchDeleting.value = true

    // 并发删除所有选中的图书
    const deletePromises = selectedBooks.value.map(book => deleteBook(book.id))
    await Promise.all(deletePromises)

    // 重新获取列表
    await getBookList(searchForm.value.name)
    selectedBooks.value = []

    ElMessage.success(`成功删除选中的图书！`)
  } catch (error: any) {
    if (error !== 'cancel') {
      ElMessage.error('批量删除失败')
    }
  } finally {
    batchDeleting.value = false
  }
}

// 新增表单 - 图片选择处理
const handleAddImageSelect: UploadProps['beforeUpload'] = (file) => {
  const isImage = file.type.startsWith('image/')
  if (!isImage) {
    ElMessage.error('只能选择图片文件!')
    return false
  }

  const isLt3M = file.size / 1024 / 1024 < 3
  if (!isLt3M) {
    ElMessage.error('图片大小不能超过 3MB!')
    return false
  }

  // 保存文件并创建预览
  addImageFile.value = file
  const reader = new FileReader()
  reader.onload = (e) => {
    addImagePreview.value = e.target?.result as string
    addForm.value.cover = 'selected' // 标记已选择图片用于验证
  }
  reader.readAsDataURL(file)

  return false // 阻止自动上传
}

// 编辑表单 - 图片选择处理
const handleEditImageSelect: UploadProps['beforeUpload'] = (file) => {
  const isImage = file.type.startsWith('image/')
  if (!isImage) {
    ElMessage.error('只能选择图片文件!')
    return false
  }

  const isLt3M = file.size / 1024 / 1024 < 3
  if (!isLt3M) {
    ElMessage.error('图片大小不能超过 3MB!')
    return false
  }

  // 保存文件并创建预览
  editImageFile.value = file
  const reader = new FileReader()
  reader.onload = (e) => {
    editImagePreview.value = e.target?.result as string
    editForm.value.cover = 'selected' // 标记已选择图片用于验证
  }
  reader.readAsDataURL(file)

  return false // 阻止自动上传
}

// 移除新增图片
const removeAddImage = () => {
  addImageFile.value = null
  addImagePreview.value = ''
  addForm.value.cover = ''
}

// 移除编辑图片
const removeEditImage = () => {
  editImageFile.value = null
  editImagePreview.value = ''
  editForm.value.cover = editForm.value.cover === 'selected' ? '' : editForm.value.cover
}

// 保存新增图书
const handleSaveAdd = async () => {
  try {
    // 数据验证
    if (!addForm.value.name?.trim()) {
      ElMessage.warning('请输入图书名称')
      return
    }
    if (!addForm.value.author?.trim()) {
      ElMessage.warning('请输入作者')
      return
    }
    if (!addForm.value.description?.trim()) {
      ElMessage.warning('请输入图书描述')
      return
    }
    if (!addImageFile.value) {
      ElMessage.warning('请选择封面图片')
      return
    }

    // 先上传图片
    const uploadRes = await uploadImage(addImageFile.value)
    const coverUrl = `/api/${uploadRes.data}`

    // 准备发送的数据
    const bookData = {
      name: addForm.value.name.trim(),
      author: addForm.value.author.trim(),
      description: addForm.value.description.trim(),
      cover: coverUrl
    }

    const res = await createBook(bookData)

    // 重新获取列表
    await getBookList(searchForm.value.name)

    ElMessage.success('添加成功！')
    addDialogVisible.value = false

    // 清空表单
    addForm.value = {
      name: '',
      author: '',
      description: '',
      cover: ''
    }
    addImageFile.value = null
    addImagePreview.value = ''
  } catch (error: any) {
    console.error('添加图书错误:', error);
    ElMessage.error(error.response?.data?.message || error.message || '添加失败')
  }
}

// 查看详情
const handleDetail = (book: Book) => {
  currentBook.value = book
  detailDialogVisible.value = true
}

// 编辑图书
const handleEdit = (book: Book) => {
  editForm.value = { ...book }
  editImageFile.value = null
  editImagePreview.value = ''
  editDialogVisible.value = true
}

// 保存编辑
const handleSaveEdit = async () => {
  try {
    if (!editForm.value.name?.trim()) {
      ElMessage.warning('请输入图书名称')
      return
    }
    if (!editForm.value.author?.trim()) {
      ElMessage.warning('请输入作者')
      return
    }
    if (!editForm.value.description?.trim()) {
      ElMessage.warning('请输入图书描述')
      return
    }
    if (!editForm.value.cover?.trim() && !editImageFile.value) {
      ElMessage.warning('请选择封面图片')
      return
    }

    let coverUrl = editForm.value.cover

    // 如果选择了新图片，先上传
    if (editImageFile.value) {
      const uploadRes = await uploadImage(editImageFile.value)
      coverUrl = `/api/${uploadRes.data}`
    }

    const updateData = {
      ...editForm.value,
      cover: coverUrl
    }

    await updateBook(updateData)

    // 更新本地数据
    const index = bookList.value.findIndex(book => book.id === editForm.value.id)
    if (index !== -1) {
      bookList.value[index] = { ...updateData }
    }

    ElMessage.success('编辑成功！')
    editDialogVisible.value = false
  } catch (error: any) {
    ElMessage.error(error.response?.data?.message || '编辑失败')
  }
}

// 删除图书
const handleDelete = async (book: Book) => {
  try {
    await ElMessageBox.confirm(
        `确定要删除图书《${book.name}》吗？`,
        '删除确认',
        {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning',
        }
    )

    await deleteBook(book.id)

    // 更新本地数据
    const index = bookList.value.findIndex(item => item.id === book.id)
    if (index !== -1) {
      bookList.value.splice(index, 1)
    }

    ElMessage.success('删除成功！')
  } catch (error: any) {
    if (error !== 'cancel') {
      ElMessage.error(error.response?.data?.message || '删除失败')
    }
  }
}

const handleImageError = (event: Event) => {
  const target = event.target as HTMLImageElement;
  if (target) {
    target.style.display = 'none';
  }
}

const getImageUrl = (url: string) => {
  if (url && url.includes('localhost:3000')) {
    return url.replace('http://localhost:3000', '/api');
  }
  return url;
};

onMounted(()=>{
  getBookList();
})
</script>

<template>
  <div class="book-manage-container">
    <!-- 页面头部 -->
    <div class="page-header">
      <h1>📚 图书管理</h1>
      <p>管理您的图书收藏，发现知识的力量</p>
    </div>

    <!-- 内容区域 - 最大宽度1200px -->
    <div class="content">
      <!-- 搜索图书卡片 -->
      <div class="search-card">
        <div class="card-header">
          <h3>🔍 搜索图书</h3>
        </div>
        <div class="search-content">
          <el-form :model="searchForm" :inline="true" class="search-form">
            <el-form-item label="图书名称">
              <el-input
                  v-model="searchForm.name"
                  placeholder="请输入图书名称..."
                  style="width: 280px"
                  clearable
                  @keyup.enter="handleSearch"
              />
            </el-form-item>
            <el-form-item>
              <el-button type="primary" @click="handleSearch" :loading="loading">
                搜索图书
              </el-button>
              <el-button @click="handleReset">重置</el-button>
              <el-button type="success" @click="handleAdd">添加图书</el-button>
              <el-button
                  type="danger"
                  @click="handleBatchDelete"
                  :disabled="selectedBooks.length === 0"
                  :loading="batchDeleting"
              >
                <el-icon><Delete /></el-icon>
                批量删除 ({{ selectedBooks.length }})
              </el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>

      <!-- 统计卡片 -->
      <div class="stats-card">
        <div class="stats-item">
          <div class="stats-icon">📚</div>
          <div class="stats-content">
            <div class="stats-number">{{ bookList.length }}</div>
            <div class="stats-label">图书总数</div>
          </div>
        </div>
      </div>

      <!-- 图书列表卡片 -->
      <div class="books-card">
        <div class="card-header">
          <h3>📖 图书列表</h3>
          <el-tag v-if="searchForm.name" type="info" closable @close="handleReset">
            搜索："{{ searchForm.name }}"
          </el-tag>
        </div>

        <!-- 加载状态 -->
        <div v-if="loading" class="loading-state">
          <el-skeleton :rows="4" animated />
        </div>

        <!-- 空状态 -->
        <div v-else-if="bookList.length === 0" class="empty-state">
          <div class="empty-icon">📚</div>
          <h3>暂无图书数据</h3>
          <p>{{ searchForm.name ? '没有找到匹配的图书，请尝试其他关键词' : '还没有添加任何图书，点击上方按钮开始添加吧！' }}</p>
          <el-button v-if="!searchForm.name" type="primary" @click="handleAdd">
            添加第一本图书
          </el-button>
          <el-button v-else @click="handleReset">
            查看全部图书
          </el-button>
        </div>

        <!-- 图书列表 -->
        <div v-else class="book-table-container">
          <el-table
              :data="bookList"
              @selection-change="handleSelectionChange"
              style="width: 100%"
              :header-cell-style="{ background: '#f5f7fa', color: '#606266' }"
          >
            <el-table-column type="selection" width="55" />
            <el-table-column label="封面" width="120">
              <template #default="{ row }">
                <div class="table-cover">
                  <img
                      :src="getImageUrl(row.cover) || 'https://8cmcy8.github.io/docs/assets/rain.BDpIq0sn.webp'"
                      alt="书籍封面"
                      @click="handleDetail(row)"
                  />
                </div>
              </template>
            </el-table-column>
            <el-table-column prop="id" label="ID" width="80" />
            <el-table-column prop="name" label="图书名称" min-width="150" show-overflow-tooltip />
            <el-table-column prop="author" label="作者" width="120" show-overflow-tooltip />
            <el-table-column prop="description" label="描述" min-width="200" />
            <el-table-column label="操作" width="200" fixed="right">
              <template #default="{ row }">
                <el-button type="primary" size="small" @click="handleDetail(row)">详情</el-button>
                <el-button type="warning" size="small" @click="handleEdit(row)">编辑</el-button>
                <el-button type="danger" size="small" @click="handleDelete(row)">删除</el-button>
              </template>
            </el-table-column>
          </el-table>
        </div>
      </div>
    </div>
  </div>

  <!-- 详情弹窗 -->
  <el-dialog
      v-model="detailDialogVisible"
      title="📖 图书详情"
      width="560px"
      :before-close="() => detailDialogVisible = false"
  >
    <div v-if="currentBook" class="book-detail">
      <div class="detail-cover">
        <img
            :src="getImageUrl(currentBook.cover) || 'https://8cmcy8.github.io/docs/assets/rain.BDpIq0sn.webp'"
            alt="书籍封面"
        />
      </div>
      <div class="detail-info">
        <h2 class="book-title">{{ currentBook.name }}</h2>
        <div class="info-item">
          <span class="info-label">📝 作者</span>
          <span class="info-value">{{ currentBook.author }}</span>
        </div>
        <div class="info-item">
          <span class="info-label">🆔 编号</span>
          <span class="info-value">#{{ currentBook.id }}</span>
        </div>
        <div class="info-item description">
          <span class="info-label">📋 描述</span>
          <div class="description-content">{{ currentBook.description }}</div>
        </div>
      </div>
    </div>
  </el-dialog>

  <!-- 编辑弹窗 -->
  <el-dialog
      v-model="editDialogVisible"
      title="✏️ 编辑图书"
      width="700px"
      :before-close="() => editDialogVisible = false"
  >
    <div class="edit-content">
      <el-form :model="editForm" :rules="formRules" label-width="120px" class="edit-form">
        <div class="form-row">
          <el-form-item label="📚 图书名称" prop="name" required class="full-width">
            <el-input
                v-model="editForm.name"
                placeholder="请输入图书名称"
                size="large"
                clearable
            />
          </el-form-item>
        </div>

        <div class="form-row">
          <el-form-item label="👤 作者" prop="author" required class="full-width">
            <el-input
                v-model="editForm.author"
                placeholder="请输入作者姓名"
                size="large"
                clearable
            />
          </el-form-item>
        </div>

        <div class="form-row">
          <el-form-item label="🖼️ 封面图片" prop="cover" required class="full-width">
            <div class="upload-container">
              <el-upload
                  :show-file-list="false"
                  :before-upload="handleEditImageSelect"
                  accept="image/*"
                  drag
                  class="upload-demo"
              >
                <div v-if="!editImagePreview && !editForm.cover" class="upload-placeholder">
                  <el-icon class="el-icon--upload"><upload-filled /></el-icon>
                  <div class="el-upload__text">
                    将图片拖到此处，或<em>点击选择</em>
                  </div>
                  <div class="el-upload__tip">
                    只能选择jpg/png文件，且不超过3MB（保存时才会上传）
                  </div>
                </div>
                <div v-else class="uploaded-image">
                  <img
                      :src="editImagePreview || getImageUrl(editForm.cover) || 'https://8cmcy8.github.io/docs/assets/rain.BDpIq0sn.webp'"
                      alt="封面预览"
                  />
                  <div class="upload-mask">
                    <el-icon><edit /></el-icon>
                    <span>{{ editImagePreview ? '重新选择' : '更换图片' }}</span>
                  </div>
                </div>
              </el-upload>
              <el-button
                  v-if="editImagePreview"
                  type="danger"
                  size="small"
                  @click="removeEditImage"
                  class="remove-btn"
              >
                移除新选择的图片
              </el-button>
            </div>
          </el-form-item>
        </div>

        <div class="form-row">
          <el-form-item label="📝 图书描述" prop="description" required class="full-width">
            <el-input
                v-model="editForm.description"
                type="textarea"
                :rows="5"
                placeholder="请输入图书描述"
                resize="none"
                show-word-limit
                maxlength="150"
            />
          </el-form-item>
        </div>
      </el-form>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button size="large" @click="editDialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" size="large" @click="handleSaveEdit">
          💾 保存修改
        </el-button>
      </div>
    </template>
  </el-dialog>

  <!-- 新增图书弹窗 -->
  <el-dialog
      v-model="addDialogVisible"
      title="➕ 添加图书"
      width="700px"
      :before-close="() => addDialogVisible = false"
  >
    <div class="edit-content">
      <el-form :model="addForm" :rules="formRules" label-width="120px" class="edit-form">
        <div class="form-row">
          <el-form-item label="📚 图书名称" prop="name" required class="full-width">
            <el-input
                v-model="addForm.name"
                placeholder="请输入图书名称"
                size="large"
                clearable
            />
          </el-form-item>
        </div>

        <div class="form-row">
          <el-form-item label="👤 作者" prop="author" required class="full-width">
            <el-input
                v-model="addForm.author"
                placeholder="请输入作者姓名"
                size="large"
                clearable
            />
          </el-form-item>
        </div>

        <div class="form-row">
          <el-form-item label="🖼️ 封面图片" prop="cover" required class="full-width">
            <div class="upload-container">
              <el-upload
                  :show-file-list="false"
                  :before-upload="handleAddImageSelect"
                  accept="image/*"
                  drag
                  class="upload-demo"
              >
                <div v-if="!addImagePreview" class="upload-placeholder">
                  <el-icon class="el-icon--upload"><upload-filled /></el-icon>
                  <div class="el-upload__text">
                    将图片拖到此处，或<em>点击选择</em>
                  </div>
                  <div class="el-upload__tip">
                    只能选择jpg/png文件，且不超过3MB（保存时才会上传）
                  </div>
                </div>
                <div v-else class="uploaded-image">
                  <img :src="addImagePreview" alt="封面预览" />
                  <div class="upload-mask">
                    <el-icon><edit /></el-icon>
                    <span>重新选择</span>
                  </div>
                </div>
              </el-upload>
              <el-button
                  v-if="addImagePreview"
                  type="danger"
                  size="small"
                  @click="removeAddImage"
                  class="remove-btn"
              >
                移除图片
              </el-button>
            </div>
          </el-form-item>
        </div>

        <div class="form-row">
          <el-form-item label="📝 图书描述" prop="description" required class="full-width">
            <el-input
                v-model="addForm.description"
                type="textarea"
                :rows="5"
                placeholder="请输入图书描述"
                resize="none"
                show-word-limit
                maxlength="150"
            />
          </el-form-item>
        </div>
      </el-form>
    </div>

    <template #footer>
      <div class="dialog-footer">
        <el-button size="large" @click="addDialogVisible = false">
          取消
        </el-button>
        <el-button type="primary" size="large" @click="handleSaveAdd">
          ➕ 添加图书
        </el-button>
      </div>
    </template>
  </el-dialog>
</template>

<style scoped>
.book-manage-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  min-height: calc(100vh - 64px);
}

/* 页面头部 */
.page-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 40px;
  border-radius: 8px;
  margin-bottom: 20px;
  text-align: center;
}

.page-header h1 {
  margin: 0 0 8px 0;
  font-size: 28px;
  font-weight: 600;
}

.page-header p {
  margin: 0;
  font-size: 16px;
  opacity: 0.9;
}

/* 卡片通用样式 */
.search-card,
.stats-card,
.books-card {
  background: white;
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
  margin-bottom: 20px;
}

.card-header {
  padding: 20px 20px 0 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  border-bottom: 1px solid #f0f0f0;
  margin-bottom: 20px;
}

.card-header h3 {
  margin: 0;
  color: #303133;
  font-size: 16px;
  font-weight: 600;
  padding-bottom: 15px;
}

/* 搜索卡片 */
.search-content {
  padding: 0 20px 20px 20px;
}

.search-form {
  display: flex;
  align-items: end;
  flex-wrap: wrap;
  gap: 12px;
}

.search-form .el-form-item {
  margin-bottom: 0;
}

.search-form .el-button + .el-button {
  margin-left: 8px;
}

/* 统计卡片 */
.stats-card {
  padding: 20px;
}

.stats-item {
  display: flex;
  align-items: center;
  gap: 16px;
}

.stats-icon {
  width: 48px;
  height: 48px;
  background: linear-gradient(135deg, #667eea, #764ba2);
  color: white;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 20px;
}

.stats-number {
  font-size: 24px;
  font-weight: 600;
  color: #303133;
  margin-bottom: 4px;
}

.stats-label {
  font-size: 14px;
  color: #909399;
}

/* 图书列表卡片 */
.books-card {
  padding-bottom: 20px;
}

.loading-state {
  padding: 20px;
}

.empty-state {
  text-align: center;
  padding: 60px 20px;
  color: #909399;
}

.empty-icon {
  font-size: 64px;
  margin-bottom: 16px;
}

.empty-state h3 {
  color: #303133;
  margin-bottom: 8px;
}

.empty-state p {
  margin-bottom: 24px;
  line-height: 1.6;
}

/* 表格相关样式 */
.book-table-container {
  padding: 0 20px 20px 20px;
}

.table-cover {
  width: 60px;
  height: 80px;
  border-radius: 4px;
  overflow: hidden;
  cursor: pointer;
  transition: transform 0.3s;
}

.table-cover:hover {
  transform: scale(1.05);
}

.table-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* 详情弹窗样式 */
.book-detail {
  display: flex;
  gap: 24px;
  padding: 8px;
}

.detail-cover {
  flex-shrink: 0;
  width: 140px;
  height: 180px;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 8px 32px rgba(0, 0, 0, 0.15);
  border: 3px solid #f0f0f0;
}

.detail-cover img {
  width: 100%;
  height: 100%;
  object-fit: cover;
  transition: transform 0.3s ease;
}

.detail-cover:hover img {
  transform: scale(1.05);
}

.detail-info {
  flex: 1;
  padding: 8px 0;
}

.book-title {
  margin: 0 0 20px 0;
  color: #2c3e50;
  font-size: 20px;
  font-weight: 600;
  line-height: 1.4;
  border-bottom: 2px solid #3498db;
  padding-bottom: 8px;
}

.info-item {
  display: flex;
  margin-bottom: 16px;
  align-items: flex-start;
}

.info-item.description {
  flex-direction: column;
  align-items: stretch;
}

.info-label {
  font-weight: 600;
  color: #34495e;
  min-width: 60px;
  margin-right: 12px;
  font-size: 14px;
}

.info-value {
  color: #2c3e50;
  line-height: 1.5;
  flex: 1;
}

.description-content {
  margin-top: 8px;
  padding: 12px;
  background: #f8f9fa;
  border-radius: 8px;
  border-left: 4px solid #3498db;
  line-height: 1.6;
  color: #2c3e50;
  font-size: 14px;
}

/* 编辑弹窗样式 */
.edit-content {
  padding: 8px;
}

.edit-form {
  max-width: 100%;
}

.form-row {
  margin-bottom: 8px;
}

.full-width {
  width: 100%;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 16px;
  padding: 16px 0 8px 0;
}

/* 上传组件样式 */
.upload-container {
  width: 100%;
}

.upload-demo {
  width: 100%;
}

.upload-placeholder {
  padding: 40px 20px;
  text-align: center;
  color: #8c939d;
  border: 2px dashed #d9d9d9;
  border-radius: 8px;
  transition: border-color 0.3s;
}

.upload-placeholder:hover {
  border-color: #409eff;
}

.el-icon--upload {
  font-size: 28px;
  margin-bottom: 16px;
  color: #8c939d;
}

.el-upload__text {
  margin-bottom: 8px;
  font-size: 14px;
}

.el-upload__text em {
  color: #409eff;
  font-style: normal;
}

.el-upload__tip {
  font-size: 12px;
  color: #8c939d;
}

.uploaded-image {
  position: relative;
  width: 100%;
  height: 200px;
  border-radius: 8px;
  overflow: hidden;
  border: 2px solid #e4e7ed;
}

.uploaded-image img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.upload-mask {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background: rgba(0, 0, 0, 0.6);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  color: white;
  opacity: 0;
  transition: opacity 0.3s;
}

.uploaded-image:hover .upload-mask {
  opacity: 1;
}

.upload-mask .el-icon {
  font-size: 24px;
  margin-bottom: 8px;
}

.remove-btn {
  margin-top: 8px;
  width: 100%;
}

/* 批量操作按钮样式 */
.search-form .el-button[type="danger"] {
  background: #f56c6c;
  border-color: #f56c6c;
}

.search-form .el-button[type="danger"]:hover {
  background: #f78989;
  border-color: #f78989;
}

.search-form .el-button[type="danger"]:disabled {
  background: #f5f5f5;
  border-color: #e4e7ed;
  color: #c0c4cc;
}

/* 弹窗整体样式优化 */
:deep(.el-dialog) {
  border-radius: 16px;
  overflow: hidden;
}

:deep(.el-dialog__header) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 20px 24px;
  margin: 0;
}

:deep(.el-dialog__title) {
  color: white;
  font-weight: 600;
  font-size: 18px;
}

:deep(.el-dialog__headerbtn) {
  top: 50%;
  transform: translateY(-50%);
}

:deep(.el-dialog__close) {
  color: white;
  font-size: 20px;
}

:deep(.el-dialog__close:hover) {
  color: #f0f0f0;
}

:deep(.el-dialog__body) {
  padding: 24px;
  background: #fafbfc;
}

:deep(.el-dialog__footer) {
  padding: 16px 24px 24px 24px;
  background: #fafbfc;
}

/* 表单样式优化 */
:deep(.el-form-item__label) {
  color: #2c3e50;
  font-weight: 600;
  line-height: 1.5;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.06);
  transition: all 0.3s ease;
}

:deep(.el-input__wrapper:hover) {
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

:deep(.el-textarea__inner) {
  border-radius: 8px;
  font-family: inherit;
  line-height: 1.6;
}

/* 按钮样式优化 */
:deep(.el-button) {
  border-radius: 8px;
  font-weight: 500;
  transition: all 0.3s ease;
}

:deep(.el-button--primary) {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  border: none;
}

:deep(.el-button--primary:hover) {
  transform: translateY(-2px);
  box-shadow: 0 8px 25px rgba(102, 126, 234, 0.4);
}

/* 优化拖拽上传样式 */
:deep(.el-upload-dragger) {
  border: 2px dashed #d9d9d9;
  border-radius: 8px;
  width: 100%;
  height: auto;
  padding: 0;
  background: #fafafa;
  transition: all 0.3s;
}

:deep(.el-upload-dragger:hover) {
  border-color: #409eff;
  background: #f5f7fa;
}

:deep(.el-upload-dragger.is-dragover) {
  border-color: #409eff;
  background: #f0f9ff;
}

/* 优化表格样式 */
:deep(.el-table) {
  border-radius: 8px;
  overflow: hidden;
}

:deep(.el-table__header) {
  background: #f5f7fa;
}

:deep(.el-table tbody tr:hover > td) {
  background-color: #f5f7fa;
}

:deep(.el-table .cell) {
  padding: 8px 12px;
}

/* 响应式适配 */
@media (max-width: 768px) {
  .book-manage-container {
    padding: 16px;
  }

  .page-header {
    padding: 24px 20px;
  }

  .page-header h1 {
    font-size: 24px;
  }

  .search-form {
    flex-direction: column;
    align-items: stretch;
  }

  .search-form .el-form-item {
    margin-bottom: 12px;
  }

  .search-form .el-button {
    margin-left: 0 !important;
    margin-right: 8px;
  }

  .card-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 12px;
  }

  .book-detail {
    flex-direction: column;
    text-align: center;
  }

  .detail-cover {
    width: 120px;
    height: 155px;
    margin: 0 auto;
  }

  .info-item {
    flex-direction: column;
    text-align: left;
    margin-bottom: 12px;
  }

  .info-label {
    margin-right: 0;
    margin-bottom: 4px;
  }
  :deep(.el-table) {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
  }
}
</style>